package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.hbpa.dao.AdminScDao;
import com.hbpa.form.StudentReg;
import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcConnectionFactory;
import com.hbpa.util.JdbcUtil;

public class AdminScDaoImpl implements AdminScDao {

	
	/*public static void main(String[] args) {
		AdminScDaoImpl a = new AdminScDaoImpl();
		a.setQueXiaosc("070212046");
	}*/
	
	public boolean setQueXiaosc(String xh) {
		Connection con = null;
		PreparedStatement ps1 = null,ps2 = null;
		try {
			con = ConnectionFactory.getConnection(); 
			con.setAutoCommit(false); 
			
			ps1 = con.prepareStatement("update login_acc set xg='0' where xh = ?;"); 
			ps1.setString(1, xh);
			ps2 = con.prepareStatement("update login_acc set glysc='0' where xh = ?;"); 
			ps2.setString(1, xh); 
			
			ps1.executeUpdate();
			ps2.executeUpdate(); 
			
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps1, con);
			JdbcUtil.close(ps2, con);
		}
		return false;
	}

	public boolean setTongGuosc(String xh) {
		Connection con = null;
		PreparedStatement ps1 = null,ps2 = null;
		try {
			con = ConnectionFactory.getConnection(); 
			con.setAutoCommit(false); 
			
			ps1 = con.prepareStatement("update login_acc set xg='1' where xh = ?;"); 
			ps1.setString(1, xh);
			ps2 = con.prepareStatement("update login_acc set glysc='1' where xh = ?;"); 
			ps2.setString(1, xh); 
			
			ps1.executeUpdate();
			ps2.executeUpdate(); 
			
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps1, con);
			JdbcUtil.close(ps2, con);
		}
		return false;
	}

	public boolean isShengheByGly(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		StudentReg st = new StudentReg();
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select glysc from login_acc where xh = ?");
			 
			ps.setString(1, xh);
			rs = ps.executeQuery();
			 
			if(rs.next())
			{ 
				 String xg = rs.getString("glysc");
				 if(xg.equals("1"))
				 {
					 return true;
			     }else{
			    	 return false;
			     }
			}
			
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		} 
		return false;
	}

}
